package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bean.UserBean;
import database.DatabaseManager;

/**
 * Data Access Object with static functions concerning the table Users within
 * the database
 * 
 * @author Sander Meijer & Stijn Kliemesch
 * 
 */
public class UserDAO {

	/**
	 * gets a userBean for the given ID
	 * 
	 * @param userID int
	 * @return {@link UserBean}
	 */
	public static UserBean getUserByID(int userID) {
		
		UserBean retBean = null;

		PreparedStatement psGetUserByUserID;
		ResultSet rsGetUserByUserID;
		String getuserByUserIDQuery = 
			"SELECT * FROM Users WHERE IDUser=?";

		try {
			psGetUserByUserID = 
				DatabaseManager.getInstance().getConnection()
					.prepareStatement(getuserByUserIDQuery);
			psGetUserByUserID.setInt(1, userID);

			rsGetUserByUserID = psGetUserByUserID.executeQuery();

			while (rsGetUserByUserID.next()) {
				String userName = rsGetUserByUserID.getString("UserName");
				String password = rsGetUserByUserID.getString("Password");
				
				retBean = new UserBean(userID, userName, password);
			}

			psGetUserByUserID.close();
			rsGetUserByUserID.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return retBean;
	}

	/**
	 * Searches for the user with the given parameter as username.
	 * 
	 * @param username String
	 * @return {@link UserBean}
	 */
	public static UserBean getUserByUsername(String username) {
		UserBean retBean = null;

		PreparedStatement psGetUserByUsername;
		ResultSet rsGetUserByUserName;
		String getUserByUsernameQuery = 
			"SELECT * FROM Users WHERE UserName=?";

		try {
			psGetUserByUsername = 
				DatabaseManager.getInstance().getConnection()
					.prepareStatement(getUserByUsernameQuery);
			psGetUserByUsername.setString(1, username);

			rsGetUserByUserName = psGetUserByUsername.executeQuery();

			while (rsGetUserByUserName.next()) {
				if (retBean != null)
					throw new IllegalStateException(
							"User already exists, found another with same name");
				
				int userID = rsGetUserByUserName.getInt("IDUser");
				String userName = rsGetUserByUserName.getString("UserName");
				String password = rsGetUserByUserName.getString("Password");

				retBean = new UserBean(userID, userName, password);
			}
			
			psGetUserByUsername.close();
			rsGetUserByUserName.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return retBean;
	}

	/**
	 * Searches for the user and returns true or false regarding it's existence.
	 * 
	 * @param username String
	 * @return {@link UserBean}
	 */
	public static boolean getUserExists(String username) {
		
		PreparedStatement psGetUserByUsername;
		ResultSet rsGetUserByUserName;
		String getUserByUsernameQuery = 
			"SELECT UserName FROM Users WHERE UserName=?";
		boolean exists = false;

		try {
			psGetUserByUsername = 
				DatabaseManager.getInstance().getConnection()
					.prepareStatement(getUserByUsernameQuery);
			psGetUserByUsername.setString(1, username);

			rsGetUserByUserName = psGetUserByUsername.executeQuery();

			while (rsGetUserByUserName.next()) {
				if (exists == true)
					throw new IllegalStateException(
							"User already exists, found another with same name");
				exists = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return exists;
	}

	/**
	 * Creates a new user with the given parameters
	 * 
	 * @param userName String
	 * @param password String
	 */
	public static void createNewUser(String userName, String password) {

		PreparedStatement psCreateNewUser;
		String createNewUserQuery = 
			"INSERT INTO Users (UserName, Password) VALUES (?, ?)";

		try {
			psCreateNewUser = 
				DatabaseManager.getInstance().getConnection()
					.prepareStatement(createNewUserQuery);
			psCreateNewUser.setString(1, userName);
			psCreateNewUser.setString(2, password);

			psCreateNewUser.execute();
			psCreateNewUser.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Updates the user with the given userID
	 * @param newPassword string
	 * @param userID int
	 */
	public static void updateUserByID(String newPassword, int userID) {

		PreparedStatement psUpdateUserByID;
		String updateUserByIDQuery = 
			"UPDATE Users SET Password=? WHERE IDUser=?";

		try {
			psUpdateUserByID = 
				DatabaseManager.getInstance().getConnection()
					.prepareStatement(updateUserByIDQuery);
			psUpdateUserByID.setString(1, newPassword);
			psUpdateUserByID.setInt(2, userID);

			psUpdateUserByID.execute();
			psUpdateUserByID.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
